<?php
/**
 * 财富相关接口
 * */

namespace App\Http\Controllers;

use App\Http\Model\SystemConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\FinanceServices;
use App\Http\Services\UserServices;

class ApiFinanceController extends BaseController
{
    protected $service;
    protected $userServices;

    public function __construct()
    {
        $this->service = new FinanceServices();
        $this->userServices = new UserServices();
    }

    /**
     * 用户购买金豆
     */
    public function jdUserBuy(Request $request){
        $user_id = (int) $request->post('user_id');
        $type = (int) $request->post('type');//类型 1=商品 2=道具
        $data_id = (int) $request->post('data_id');
        $num = (int) $request->post('num');//数量
        $price = (int) $request->post('price');//单价钻石
        $timeLimit = (int) $request->post('time_limit');//有效期秒
        if(empty($user_id)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if(empty($data_id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if($num < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //商品
        if($type == 1) {
            $data = $this->service->buyGoodGetJd($user_id, $data_id, $num, $price, $timeLimit);
            if($data['code'] != Hint::SUCCESS) {
                Response::returnResult($data['code'], $data['data'], $data['msg']);
            }
        }
        //商品
        if($type == 2) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 获取私聊扣费配置
     */
    public function msgPayConfig(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->service->msgPayConfig($userInfo['user_id']);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 支付语音通话费用
     */
    public function payVoiceCall(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $callUserId = $request->post('call_user_id');
        if(empty($callUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->payVoiceCall($userInfo['user_id'], $callUserId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        //修改主播状态
        $this->userServices->identityStatusUpdate($userInfo['user_id'], 3, ['identity_status'=>1]);

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 支付私聊费用
     */
    public function payPrivateMsg(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $callUserId = $request->post('call_user_id');
        if(empty($callUserId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->payPrivateMsg($userInfo['user_id'], $callUserId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 获取财富等级列表
     */
    public function wealth_rank_list()
    {
        $rank_data = $this->service->getWealthRankList();
        Response::returnResult(Hint::SUCCESS, $rank_data);
    }

    /**
     * 获取魅力等级列表
     */
    public function charm_rank_list()
    {
        $rank_data = $this->service->getCharmRankList();
        Response::returnResult(Hint::SUCCESS, $rank_data);
    }

    /**
     * 获取娱乐等级列表
     */
    public function game_rank_list()
    {
        $rank_data = $this->service->getGameRankList();
        Response::returnResult(Hint::SUCCESS, $rank_data);
    }

    /**
     * 获取用户娱乐等级
     */
    public function getUserGameRank(Request $request){
        $userId = $request->input('user_id');
        $userInfo = $this->userServices->get_current_user();
        $userId = empty($userInfo['user_id']) ? $userId : $userInfo['user_id'];
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $data = $this->service->getUserGameRank($userId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 用户领取娱乐等级奖励
     */
    public function rewardUserGameRank(Request $request){
        $userId = $request->input('user_id');
        $userInfo = $this->userServices->get_current_user();
        $userId = empty($userInfo['user_id']) ? $userId : $userInfo['user_id'];
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $level = $request->input('level', 0);

        $data = $this->service->rewardUserGameRank($userId, $level);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 用户代理状态
     */
    public function agentStatus(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $data = $this->service->agentStatus($userId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 代理发验证码
     */
    public function agentSendSms(Request $request){
        $userId = $this->getLoginUserId();
        $sms_token = $request->get('sms_token','');
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $data = $this->service->agentSendSms($userId, $sms_token);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 代理设置密码
     */
    public function agentSetPd(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $password = trim($request->input('password'));
        $sms_code = $request->input('sms_code');
        if(empty($password) || empty($sms_code)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->agentSetPd($userId, $password, $sms_code);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 代理金币兑换钻石
     */
    public function agentKkExchangeDiamond(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $kkNum = (int) $request->input('kk_num');
        if(empty($kkNum) || $kkNum<1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->agentKkExchangeDiamond($userInfo['user_id'], $kkNum);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 代理转钻石到指定用户
     */
    public function agentDiamondToUser(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $toUuid = (int) $request->input('to_uuid');
        $price = (int) $request->input('price');//转出钻石数量
//        $sms_code = $request->input('sms_code');
//        $password = trim($request->input('password'));
//        if(empty($toUuid) || empty($price) || (empty($sms_code) && empty($password)) || $price<1) {
        if(empty($toUuid) || empty($price)|| $price<1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if($price < 1) {
            Response::returnResult(Hint::PARAM_ERROR, [], '钻石数量不可小于1');
        }

        //转出钻石数量限制
        $transfer_diamond_max = (new SystemConfig())->getKeyConfig('transfer_diamond_max')[0];
        if ($transfer_diamond_max && $transfer_diamond_max < $price) {
            Response::returnResult(Hint::PARAM_ERROR, [], '转出钻石数量不可大于' . $transfer_diamond_max);
        }


//        $data = $this->service->agentDiamondToUser($userInfo['user_id'], $toUuid, $price, $sms_code, $password);
        $data = $this->service->agentDiamondToUser($userInfo['user_id'], $toUuid, $price);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 用户领取转入的钻石
     */
    public function userReceiveAgentDiamond(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $logId = (int) $request->input('logId');
        if(empty($logId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->userReceiveAgentDiamond($userInfo['user_id'], $logId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 获取用户代理交易记录
     */
    public function userAgentDLog(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $page = !empty((int)$request->input('page_index')) && $request->input('page_index') >= 0 ? (int)$request->input('page_index') : 0;
        $page_size = !empty((int)$request->input('page_size')) && $request->input('page_size') > 0 ? (int)$request->input('page_size') : 10;
        $streamType = $request->input('agent_type');//交易类型 1=转出 2=兑换
        $timeSt = $request->input('time_st');//开始时间 时间戳
        $timeEd = $request->input('time_ed');//结束时间 时间戳

        $data = $this->service->userAgentDLog($page, $page_size, $userInfo['user_id'], $streamType, $timeSt, $timeEd);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 金币兑换金豆
     */
    public function kkBuyJd(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $num = (int) $request->post('num');//数量
        if($num < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->kkBuyJd($userInfo['user_id'], $num);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 用户金豆流水列表
     */
    public function jdDetailList(Request $request)
    {
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $page = !empty((int)$request->get('page_index')) && $request->get('page_index') >= 0 ? (int)$request->get('page_index') : 0;
        $page_size = !empty((int)$request->get('page_size')) && $request->get('page_size') > 0 ? (int)$request->get('page_size') : 10;

        $streamList = $this->service->jdDetailList($userInfo['user_id'], $page, $page_size);

        Response::returnResult(Hint::SUCCESS, $streamList);
    }

    /**
     * 代理转金币到指定用户
     */
    public function agentGolodToUser(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $toUuid = (int) $request->input('to_uuid');
        $price = (int) $request->input('price');//转出钻石数量
//        $sms_code = $request->input('sms_code');
//        $password = trim($request->input('password'));
//        if(empty($toUuid) || empty($price) || (empty($sms_code) && empty($password)) || $price<1) {
        if(empty($toUuid) || empty($price)|| $price<1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if($price < 1) {
            Response::returnResult(Hint::PARAM_ERROR, [], '金币数量不可小于1');

        }

        //转出金币数量限制
        $transfer_diamond_max = (new SystemConfig())->getKeyConfig('transfer_kk_max')[0];
        if ($transfer_diamond_max && $transfer_diamond_max < $price) {
            Response::returnResult(Hint::PARAM_ERROR, [], '转出金币数量不可大于' . $transfer_diamond_max);
        }


//        $data = $this->service->agentDiamondToUser($userInfo['user_id'], $toUuid, $price, $sms_code, $password);
        $data = $this->service->agentGolodToUser($userInfo['user_id'], $toUuid, $price);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }
    /**
     * 获取用户代理转金币交易记录
     */
    public function userAgentGLog(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $page = !empty((int)$request->input('page_index')) && $request->input('page_index') >= 0 ? (int)$request->input('page_index') : 0;
        $page_size = !empty((int)$request->input('page_size')) && $request->input('page_size') > 0 ? (int)$request->input('page_size') : 10;
        $streamType = $request->input('agent_type');//交易类型 1=转出 2=兑换
        $timeSt = $request->input('time_st');//开始时间 时间戳
        $timeEd = $request->input('time_ed');//结束时间 时间戳
        $data = $this->service->userAgentGLog($page, $page_size, $userInfo['user_id'], $streamType, $timeSt, $timeEd);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }


    /**
     * 用户领取转入的金币
     */
    public function userReceiveAgentGolod(Request $request){
        $userInfo = $this->userServices->get_current_user();
        if(empty($userInfo['user_id'])) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $logId = (int) $request->input('logId');
        if(empty($logId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->service->userReceiveAgentGolod($userInfo['user_id'], $logId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }
    /**
     * 用户金币代理状态
     */
    public function agentGolodStatus(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $data = $this->service->agentGolodStatus($userId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

}
